﻿using Best.SmartCms.Dao;
using Best.SmartCms.Domain;
using Best.SmartCms.Domain.Common;
using Best.SmartCms.Dto.Common;
using Best.SmartCms.Utility;

namespace Best.SmartCms.Service.Common
{
    /// <summary>
    /// 站点配置
    /// </summary>
    public class SiteSettingService : IService
    {
        private readonly SiteSettingDao _siteSettingDao;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mySqlDbContext"></param>
        public SiteSettingService(MySqlDbContext mySqlDbContext)
        {
            _siteSettingDao = new SiteSettingDao(mySqlDbContext);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public SiteSettingDto Info()
        {
            var entity = _siteSettingDao.GetSingle(1);
            var dto = TinyMapperExtensions.Map<SiteSetting, SiteSettingDto>(entity);
            return dto;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dto"></param>
        public void Edit(SiteSettingDto dto)
        {
            var entity = _siteSettingDao.GetSingle(1);
            if (entity == null)
            {
                entity = TinyMapperExtensions.Map<SiteSettingDto, SiteSetting>(dto);
                _siteSettingDao.Add(entity);
            }
            else
            {
                entity.SiteName = dto.SiteName;
                entity.DomainUrl = dto.DomainUrl;
                entity.DomainCaseNumber = dto.DomainCaseNumber;
                entity.CompanyName = dto.CompanyName;
                entity.CompanyAdress = dto.CompanyAdress;
                entity.CompanyPhone = dto.CompanyPhone;
                entity.Title = dto.Title;
                entity.Meta = dto.Meta;
                _siteSettingDao.Update(entity);
            }
        }
    }
}
